package com.zcp.notepad;

import com.zcp.notepad.NotePad.Notes;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import java.util.LinkedList;

public class NoticeBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
//        Log.i("222", "NoticeBroadcastReceiver");
        if("com.zcp.notePad.notice".equals(intent.getAction()))
        {
            String note_id=intent.getStringExtra("noticeId");
            
//            Log.i("222", "NoticeBroadcastReceiver1"+note_id);
//            Toast.makeText(context, "{"+note_id+"}", Toast.LENGTH_SHORT).show();
            
            String title=context.getString(R.string.app_name);
            
            Notification nfc=new Notification(R.drawable.icon,title,System.currentTimeMillis());
            nfc.flags=Notification.FLAG_AUTO_CANCEL;
            nfc.defaults=Notification.DEFAULT_ALL; //天际对于权限<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
            
            
            //设置点击通知的时候要执行的动作
            Long id=Long.valueOf(note_id);
            Intent myIntent = new Intent(context, NoteEditorActivity.class);
            Uri uri = ContentUris.withAppendedId(Notes.CONTENT_URI, id);
            //解决已经打开，时候点击通知打开的是旧的 28
            myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            myIntent.setData(uri);
            myIntent.putExtra("isShortcut", true);//这个要设置。
            myIntent.putExtra("isFromAlarm", true);
            myIntent.setAction(Intent.ACTION_EDIT);
            
            //启动通知
            String NoteTitle=Util.getNoteTitleById(context,id);
            PendingIntent nfcIntent=PendingIntent.getActivity(context, 0, myIntent, 0);
            nfc.setLatestEventInfo(context, title, NoteTitle, nfcIntent);
            NotificationManager nfcManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            nfcManager.notify(Integer.valueOf(note_id), nfc);
        }else if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
        {
//            Log.i("222", "接收到开机广播");
            
            LinkedList<Note> noteList=Util.getAllNotes(context);
//            Log.i("222", "记事本数量"+noteList.size());
            if (noteList != null && noteList.size() > 0) {                
                AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
                long currentTime = System.currentTimeMillis();
                long remindTime = 0;
                for (Note note : noteList) {
                    remindTime = Long.valueOf(note.remindTime);
                    
//                    Log.i("222", note.title+" "+note.isRemind+" "+remindTime+"[currentTime]"+currentTime);
                    
                    //如果有设置提醒，由于关机以后AlarmManager 原先设置的通知都会被清除。
                    if (note.isRemind && (remindTime > currentTime)) {
                        am.set(AlarmManager.RTC_WAKEUP, remindTime,
                                Util.getNoticPin(context, note.id + ""));
//                        Log.i("222", "重新设置提醒 "+note.title);
                    }
                }
                
                noteList.clear();
                noteList=null;
            }
            
          
            
        }
    }

}
